package com.green.service.common.log;

import java.lang.reflect.Field;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.green.dao.manage.OperateLogDao;
import com.green.model.Entity;
import com.green.model.annotation.OperateLogField;
import com.green.model.manage.OperateLog;


/** 
 * 
 * @author Gou Lvbin
 * @time 2013-10-30
 */

@Component("generalOperateLogger")
public class GeneralOperateLogger extends BaseOperateLogger implements OperateLogger{
	
	@Resource
	private OperateLogDao operateLogDao;
	
	public String getAddOperateContent(Field[] fields,Object obj) throws Exception{
		StringBuffer content=new StringBuffer();		
		for (Field field : fields) {
			if(field.isAnnotationPresent(OperateLogField.class)){
				OperateLogField operateLogField=field.getAnnotation(OperateLogField.class);
				if(obj!=null){
					String value = getValue(field, obj);
					if(StringUtils.isNotBlank(value)){
						content.append(operateLogField.desc()).append(":").append(value);
						content.append(",");
					}
				}
			}
		}
		if(StringUtils.isNotBlank(content.toString())){
			content.replace(content.length()-1, content.length(), "");
			content=new StringBuffer("新增，").append(content.toString());
		}
		return content.toString();
	}
	
	/**
	 * 操作修改
	 * @param fields
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String getUpdateOperateContent(Field[] fields,Object obj,Object historyObj) throws Exception{
		StringBuffer content=new StringBuffer();
		for (Field field : fields) {
			if(field.isAnnotationPresent(OperateLogField.class)){
				OperateLogField operateLogField=field.getAnnotation(OperateLogField.class);
				String temp1=getValue(field, obj);
				String temp2=getValue(field, historyObj);
				temp2=StringUtils.isNotBlank(temp2)?temp2:"空";
				if(StringUtils.isNotBlank(temp1)&&(!temp1.equals(temp2))){				
					content.append(operateLogField.desc()).append(":").append("从 ").append(temp2).append(" 修改为 ").append(temp1);
					content.append(",");
				}else{
					content.append(operateLogField.desc()).append(":").append(temp2);
					content.append(",");
				}
			}
		}
		if(StringUtils.isNotBlank(content.toString())){
			content.replace(content.length()-1, content.length(), "");
			content=new StringBuffer("修改,").append(content.toString());
		}
		return content.toString();
	}
	
	public String  getDelOperateContent(Field[] fields,Object historyObj)throws Exception{
		StringBuffer content=new StringBuffer();
		content.append("删除，");
		for (Field field : fields) {
			if(field.isAnnotationPresent(OperateLogField.class)){
				OperateLogField operateLogField=field.getAnnotation(OperateLogField.class);
				if("1".equals(operateLogField.isdel())){
					String value=getValue(field, historyObj);
					content.append(operateLogField.desc()).append(" :   ").append(value);
					content.append(",");
				}
			}
		}
		Object valueId= historyObj.getClass().getMethod("getId").invoke(historyObj, null);
		content.append(",编号").append(" : ").append(valueId);
		return content.toString();
		
	}

	@SuppressWarnings("rawtypes")
	public void doLog(Entity log) throws Exception {
		OperateLog operateLog  = (OperateLog) log;
		operateLogDao.insertOperateLog(operateLog);
	}
	
}
